<?php

include "../init.php";
include DIR_CORE."/MySQLDB.php";
include DIR_CORE."/upload.php";

// echo "<pre>"; // 使输出格式更易读
// print_r($_FILES);
// echo "</pre>";

$file = $_FILES['image'];
$allow = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif');
$path = DIR_UPLOADS."/images";
$error = "";

// 调用上传函数
$result = upload($file, $allow, $error, $path);

// 判断是否上传成功
if ($result) {
    session_start();
    $user_name = $_SESSION["userInfo"]['user_name'];
    // 提取用户头像
    $old_sql = "select user_image from user where user_name = '$user_name'";
    $old_name = fetchColumn($old_sql);
    // 入库
    $sql = "update user set user_image = '$result' where user_name = '$user_name'";
    $res = my_query($sql);
    if ($res) {
        unlink($path."/".$old_name);
        jump('./list_father.php', "头像上传成功");
    } else {
        jump("./upload_img.php", "头像上传失败");
    }
} else {
    // 上传失败，返回失败信息
    jump("./upload_img.php", "发生未知错误，请稍后再试");
}

?>